<?php
class DatabaseObject_Album extends DatabaseObject {
	public function __construct($db) {
		parent::__construct($db, 'albums', 'albumid');
		
		$this->add('userid', 0);
		$this->add('username', '');
		$this->add('albumname', '');
		$this->add('friend', 0);
		$this->add('dateline', 0);
		$this->add('coverid', 0);
	}
	
	static public function _GetBaseQuery($db, $options) {
		$defaults = array('userid' => array(),
		                  'from'   => '',
		                  'to'     => '');
		
		foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }
        
        $select = $db->select();
        $select->from(array('a' => 'albums'), array());
        
        if (strlen($options['from']) > 0) {
            $select->where('a.dateline <= ?', $options['from']);
        }
        
        if (strlen($options['to']) > 0) {
            $select->where('a.dateline >= ?', $options['to']);
        }
        
        if (count($options['userid']) > 0) {
            $select->where('a.userid in (?)', $options['userid']);
        }
        return $select;
	}
	static public function GetAlbumCount($db, $options) {
		$select = self::_GetBaseQuery($db, $options);
		$select->from(null, 'count(*)');
		return $db->fetchOne($select);
	}
	
	static public function GetAlbums($db, $options = array()) {
		$defaults = array('offset'  => 0,
		                  'limit'   => 0,
		                  'order'   => 'a.dateline');
		
		foreach ($defaults as $k => $v) {
            $options[$k] = array_key_exists($k, $options) ? $options[$k] : $v;
        }
        
        $select = self::_GetBaseQuery($db, $options);
        $select->from(null, 'a.*');
        
        if ($options['limit'] > 0) {
            $select->limit($options['limit'], $option['offset']);
        }
        
        $select->order($options['order']);
        
        $data = $db->fetchAll($select);
        $albums = self::BuildMultiple($db, __CLASS__, $data);
        $albumids = array_keys($albums);
        if (count($albumids) == 0) {
            return array();
        }

        return $albums;
	}
}
?>